/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controladoresJPA;

import controladoresJPA.exceptions.NonexistentEntityException;
import controladoresJPA.exceptions.RollbackFailureException;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import javax.transaction.UserTransaction;
import modelo.OperarConsumo;
import modelo.Ingreso;
import modelo.OperarConsumoIngreso;

/**
 *
 * @author Francisco
 */
public class OperarConsumoIngresoJpaController implements Serializable {

    public OperarConsumoIngresoJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(OperarConsumoIngreso operarConsumoIngreso) throws RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            OperarConsumo idOperarConsumo = operarConsumoIngreso.getIdOperarConsumo();
            if (idOperarConsumo != null) {
                idOperarConsumo = em.getReference(idOperarConsumo.getClass(), idOperarConsumo.getIdOperarConsumo());
                operarConsumoIngreso.setIdOperarConsumo(idOperarConsumo);
            }
            Ingreso idIngreso = operarConsumoIngreso.getIdIngreso();
            if (idIngreso != null) {
                idIngreso = em.getReference(idIngreso.getClass(), idIngreso.getIdIngreso());
                operarConsumoIngreso.setIdIngreso(idIngreso);
            }
            em.persist(operarConsumoIngreso);
            if (idOperarConsumo != null) {
                idOperarConsumo.getOperarConsumoIngresoList().add(operarConsumoIngreso);
                idOperarConsumo = em.merge(idOperarConsumo);
            }
            if (idIngreso != null) {
                idIngreso.getOperarConsumoIngresoList().add(operarConsumoIngreso);
                idIngreso = em.merge(idIngreso);
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(OperarConsumoIngreso operarConsumoIngreso) throws NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            OperarConsumoIngreso persistentOperarConsumoIngreso = em.find(OperarConsumoIngreso.class, operarConsumoIngreso.getIdOperarConsumoIngreso());
            OperarConsumo idOperarConsumoOld = persistentOperarConsumoIngreso.getIdOperarConsumo();
            OperarConsumo idOperarConsumoNew = operarConsumoIngreso.getIdOperarConsumo();
            Ingreso idIngresoOld = persistentOperarConsumoIngreso.getIdIngreso();
            Ingreso idIngresoNew = operarConsumoIngreso.getIdIngreso();
            if (idOperarConsumoNew != null) {
                idOperarConsumoNew = em.getReference(idOperarConsumoNew.getClass(), idOperarConsumoNew.getIdOperarConsumo());
                operarConsumoIngreso.setIdOperarConsumo(idOperarConsumoNew);
            }
            if (idIngresoNew != null) {
                idIngresoNew = em.getReference(idIngresoNew.getClass(), idIngresoNew.getIdIngreso());
                operarConsumoIngreso.setIdIngreso(idIngresoNew);
            }
            operarConsumoIngreso = em.merge(operarConsumoIngreso);
            if (idOperarConsumoOld != null && !idOperarConsumoOld.equals(idOperarConsumoNew)) {
                idOperarConsumoOld.getOperarConsumoIngresoList().remove(operarConsumoIngreso);
                idOperarConsumoOld = em.merge(idOperarConsumoOld);
            }
            if (idOperarConsumoNew != null && !idOperarConsumoNew.equals(idOperarConsumoOld)) {
                idOperarConsumoNew.getOperarConsumoIngresoList().add(operarConsumoIngreso);
                idOperarConsumoNew = em.merge(idOperarConsumoNew);
            }
            if (idIngresoOld != null && !idIngresoOld.equals(idIngresoNew)) {
                idIngresoOld.getOperarConsumoIngresoList().remove(operarConsumoIngreso);
                idIngresoOld = em.merge(idIngresoOld);
            }
            if (idIngresoNew != null && !idIngresoNew.equals(idIngresoOld)) {
                idIngresoNew.getOperarConsumoIngresoList().add(operarConsumoIngreso);
                idIngresoNew = em.merge(idIngresoNew);
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = operarConsumoIngreso.getIdOperarConsumoIngreso();
                if (findOperarConsumoIngreso(id) == null) {
                    throw new NonexistentEntityException("The operarConsumoIngreso with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            OperarConsumoIngreso operarConsumoIngreso;
            try {
                operarConsumoIngreso = em.getReference(OperarConsumoIngreso.class, id);
                operarConsumoIngreso.getIdOperarConsumoIngreso();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The operarConsumoIngreso with id " + id + " no longer exists.", enfe);
            }
            OperarConsumo idOperarConsumo = operarConsumoIngreso.getIdOperarConsumo();
            if (idOperarConsumo != null) {
                idOperarConsumo.getOperarConsumoIngresoList().remove(operarConsumoIngreso);
                idOperarConsumo = em.merge(idOperarConsumo);
            }
            Ingreso idIngreso = operarConsumoIngreso.getIdIngreso();
            if (idIngreso != null) {
                idIngreso.getOperarConsumoIngresoList().remove(operarConsumoIngreso);
                idIngreso = em.merge(idIngreso);
            }
            em.remove(operarConsumoIngreso);
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<OperarConsumoIngreso> findOperarConsumoIngresoEntities() {
        return findOperarConsumoIngresoEntities(true, -1, -1);
    }

    public List<OperarConsumoIngreso> findOperarConsumoIngresoEntities(int maxResults, int firstResult) {
        return findOperarConsumoIngresoEntities(false, maxResults, firstResult);
    }

    private List<OperarConsumoIngreso> findOperarConsumoIngresoEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(OperarConsumoIngreso.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public OperarConsumoIngreso findOperarConsumoIngreso(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(OperarConsumoIngreso.class, id);
        } finally {
            em.close();
        }
    }
    
    public List<OperarConsumoIngreso> findConsumoIngresosByIdUsuario(Integer idUsuario, Date fechaInicio, Date fechaFin) throws Exception{
        EntityManager em = getEntityManager();
        try{
            TypedQuery<OperarConsumoIngreso> tq = em.createQuery(
                    "Select oc from OperarConsumoIngreso oc where oc.idOperarConsumo.idUsuario.idUsuario = :idUsu and oc.idOperarConsumo.fechaConsumo between :fechIni and :fechFin", OperarConsumoIngreso.class);
            tq.setParameter("idUsu", idUsuario);
            tq.setParameter("fechIni", fechaInicio);
            tq.setParameter("fechFin", fechaFin);
            return tq.getResultList();
        }finally {
            em.close();
        }
    }
    
    public List<OperarConsumoIngreso> findConsumoIngresosLigByIdUsuario(Integer idUsuario, Date fechaInicio, Date fechaFin) throws Exception{
        EntityManager em = getEntityManager();
        try{
            TypedQuery<OperarConsumoIngreso> tq = em.createQuery(
                    "Select oc from OperarConsumoIngreso oc, LigaUsuarioConsumidor lu "
                    + " where oc.idIngreso.idConsumidor.idConsumidor = lu.idConsumidor.idConsumidor "
                    + " and lu.idUsuario.idUsuario = :idUsu "
                    + " and oc.idOperarConsumo.fechaConsumo between :fechIni and :fechFin", OperarConsumoIngreso.class);
            tq.setParameter("idUsu", idUsuario);
            tq.setParameter("fechIni", fechaInicio);
            tq.setParameter("fechFin", fechaFin);
            return tq.getResultList();
        }finally {
            em.close();
        }
    }

    public int getOperarConsumoIngresoCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<OperarConsumoIngreso> rt = cq.from(OperarConsumoIngreso.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
